﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for UserDAL
/// </summary>
public class UserDAL
{
    DABasic _dabsic;
    public UserDAL()
    {
        _dabsic = new DABasic();
    }

    public int register(string @username, string @password, string @email, int @bankid, string @firstname, string @lastname, string @address, Int64 @phonenumber)
    {
        string _query = "User_register";
        SqlParameter[] _para = new SqlParameter[9];
        _para[0] = new SqlParameter("@username", SqlDbType.VarChar, -1); _para[0].Value = @username;
        _para[1] = new SqlParameter("@password", SqlDbType.VarChar, -1); _para[1].Value = @password;
        _para[2] = new SqlParameter("@email", SqlDbType.VarChar, -1); _para[2].Value = @email;
        _para[3] = new SqlParameter("@bankid", SqlDbType.Int); _para[3].Value = @bankid;
        _para[4] = new SqlParameter("@firstname", SqlDbType.VarChar, -1); _para[4].Value = @firstname;
        _para[5] = new SqlParameter("@lastname", SqlDbType.VarChar, -1); _para[5].Value = @lastname;
        _para[6] = new SqlParameter("@address", SqlDbType.VarChar, -1); _para[6].Value = @address;
        _para[7] = new SqlParameter("@phonenumber", SqlDbType.BigInt); _para[7].Value = @phonenumber;
        _para[8] = new SqlParameter("@userid", SqlDbType.VarChar, -1); _para[8].Direction = ParameterDirection.Output;

        SqlParameterCollection _collection = _dabsic.ExecuteGetOutput(_query, _para, CommandType.StoredProcedure);

        return Convert.ToInt32(_collection["@userid"].Value);
    }

    public string check(string @username, string @email)
    {
        string _query = "User_check";
        SqlParameter[] _para = new SqlParameter[3];
        _para[0] = new SqlParameter("@username", SqlDbType.VarChar, -1);
        _para[0].Value = @username;
        _para[1] = new SqlParameter("@email", SqlDbType.VarChar, -1);
        _para[1].Value = @email;
        _para[2] = new SqlParameter("@warn", SqlDbType.VarChar, -1);
        _para[2].Direction = ParameterDirection.Output;
        SqlParameterCollection _collection = _dabsic.ExecuteGetOutput(_query, _para, CommandType.StoredProcedure);

        return Convert.ToString(_collection["@warn"].Value);
    }

    public int login(string @username, string @password)
    {
        string _query = "User_login";

        SqlParameter[] _para = new SqlParameter[3];
        _para[0] = new SqlParameter("@username", SqlDbType.VarChar, -1); 
        _para[0].Value = @username;
        _para[1] = new SqlParameter("@password", SqlDbType.VarChar, -1); 
        _para[1].Value = @password;
        _para[2] = new SqlParameter("@userid", SqlDbType.Int); 
        _para[2].Direction = ParameterDirection.Output;

        SqlParameterCollection output = _dabsic.ExecuteGetOutput(_query, _para, CommandType.StoredProcedure);
        return Convert.ToInt32(output["@userid"].Value);
    }

    public string getusername(int @userid)
    {
        string _query = "User_getUsername";

        SqlParameter[] _para = new SqlParameter[2];
        _para[0] = new SqlParameter("@userid", SqlDbType.Int);
        _para[0].Value = @userid;
        _para[1] = new SqlParameter("@username", SqlDbType.VarChar, -1);
        _para[1].Direction = ParameterDirection.Output;

        SqlParameterCollection output = _dabsic.ExecuteGetOutput(_query, _para, CommandType.StoredProcedure);

        return output["@username"].Value.ToString();
    }
}
